/// <reference path="../../apicloud.d.ts" />

const vm = new Vue({
    el: '#main',
    data: {
        ROLE: localStorage.getItem('_role') ? localStorage.getItem('_role') : '0',
        info: {
            nickName: '',
        },
        landlordInfo: {
            GrossIncome: {
                currentMonthIncome: '0',
            },
            comment: {
                commentScore: '0.00',
                commentCount: '0',
            },
            defaultAccountType: '支付宝',
        },
        container: {
            share: false,
        },
        role: {
            is_landlord: '0',
            id_verify: '0',
        },
    },
    methods: {
        containerShare() {
            goPage('./share_discounts.html')
        },
        becomeLandlord,
        share,
        addHouse,
        switchRole,
        openF,
    },
});

function reqMeInfo() {
    reqPersonalInformation(2, true);
}

function main() {
    const settingEl = document.getElementById('setting');
    let height = settingEl.clientHeight;
    if (fxj.getSystemType() === 'ios') {
        settingEl.style.paddingTop = 30 + 'px';
        height += 20;
    } else {
        settingEl.style.paddingTop = 35 + 'px';
        height += 25;
    }
    settingEl.style.minHeight += height + 'px';
    settingEl.style.maxHeight += height + 'px';
    reqMeInfo();
    reqRole();
    document.documentElement.addEventListener('webkitAnimationEnd', () => {
        document.documentElement.classList.remove('cube');
    });
}

if (fxj.deBug) {
    main()
}

function apiready() {
    main();
    setCustomRefreshHeaderInfo(() => {
        if (!localStorage.getItem('token')) {
            goPage('log_in.html');
            return;
        }
        reqPersonalInformation(localStorage.getItem('_role') === '0' ? '2' : '1', false);
        reqRole();
    });
}

async function reqPersonalInformation(role, first = false) {
    const result = await ajax({
        url: 'home/userCenter/personalInformation',
        type: 'POST',
        data: {
            role,
        },
    });
    if (first && vm.role.is_landlord === '1') {
        reqPersonalInformation(1);
    }
    if (result.err === 0) {
        if (parseInt(role) === 2) {
            vm.info = result.data;
        } else {
            vm.landlordInfo = result.data;
        }
    } else {
        toast(result.msg);
    }
}

async function reqRole() {
    if (!localStorage.getItem('token')) return;
    const res = await ajax({
        url: 'home/userCenter/isLandlord',
    });
    vm.role = res.data;
    if (vm.role.is_landlord === '0') {
        vm.ROLE = '0';
        localStorage.setItem('_role', '0');
    }
}

function newShareOptions(type) {
    const options = {
        title: '分享网页的标题',
        description: '分享网页的描述',
    };
    if (type === 'wb' || type === 'wx') {
        options.thumb = '../image/images/logo70.png';
        options.contentUrl = 'me.html';
        type === 'wb' && (options.text = '分享的文本测试');
        type === 'wx' && (options.scene = 'timeline');
    }
    if (type === 'qq') {
        options.url = 'me.html';
        options.imgUrl = '../image/images/logo70.png';
        options.type = 'QZone';
    }
    return options;
}

function share(type) {
    const wb = api.require('weibo');
    const qq = api.require('qq');
    const wx = api.require('wx');
    const options = newShareOptions(type);
    const cb = (res, err) => {
        console.log(res, err);
    };
    type === 'wb' && wb.shareWebPage(options, cb);
    type === 'qq' && qq.shareNews(options, cb);
    type === 'wx' && wx.shareWebpage(options, cb);
}

async function becomeLandlord() {
    const res = await ajax({
        url: 'home/userCenter/phoneIsNull',
    });
    console.log(res);
    if (vm.role.id_verify === '0') {
        goPage('evpi.html');
    } else if (vm.role.is_landlord === '0') {
        goPage('yollon_add.html');
    }
}

const stepMap = new Map([
    ['2', 'hm_introduce.html'],
    ['3', 'hm_picture.html'],
    ['4', 'hm_rull.html'],
]);

async function addHouse() {
    const close = fxj.dialog.loading({
        noBg: true
    });
    const res = await ajax({
        url: 'home/houseUpload/haveUnpublished'
    });

    if (res.err === 0) {
        goPage('hm_basic.html', {
            data: {
                type: 'add',
            },
        });
    } else {
        goPage(stepMap.get(res.data.step), {
            data: {
                type: 'add',
                houseId: res.data.houseId,
            },
        });
    }
    close();
}

function switchRole(role) {
    localStorage.setItem('_role', role);
    document.documentElement.classList.add('cube');
    reqPersonalInformation(role === '0' ? '2' : '1', false);
    setTimeout(() => {
        vm.ROLE = role;
        if (apiOk()) {
            api.execScript({
                name: 'root',
                script: `switchRole()`,
            });
        }
    }, 500);
}

function update() {
    reqPersonalInformation(2, true);
}

function openF(html, i) {
    if (i > 1 && !localStorage.getItem('token')) {
        openWin({
            url: 'html/log_in.html',
        });
        return;
    }
    vm[vm.current] = false;
    vm['tab' + i] = true;
    vm.current = 'tab' + i;
    var y = 0;
    var h = document.body.offsetHeight - document.getElementById('tab-nav').offsetHeight;
    if (typeof api !== 'undefined') {
        openFrame({
            url: html,
            bounces: true,
            rect: {
                x: 0,
                y: y,
                w: 'auto',
                h: h,
            },
        });
    } else {
        location.assign(html);
    }
}

function contactService() {
    const a = document.createElement('a');
    a.setAttribute('href', 'tel:4001177178');
    a.click();
    contactService = () => {
        a.click();
    };
}

function goYollon() {
    if (vm.role.id_verify === '0') {
        fxj.dialog.alert({
            btn: 1,
            title: '提示',
            msg: '根据相关法律法规, 请务必填写姓名和身份证号',
            leftCallback: () => {
                goPage('evpi.html');
            },
            btnLeft: '立即填写',
        });
    } else if (vm.role.id_verify === '1') {
        goPage('yollon_add.html');
    }
}